package com.app.shortvideo.ui.main

import android.app.AlertDialog
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.recyclerview.widget.GridLayoutManager
import com.app.shortvideo.base.BaseFragment
import com.app.shortvideo.config.Constant
import com.app.shortvideo.databinding.FragmentHomeOneBinding
import com.app.shortvideo.ui.main.adapter.DramaListAdapter
import com.app.shortvideo.ui.viewmodel.AddressViewModel
import com.blankj.utilcode.util.SPUtils
import com.bytedance.sdk.shortplay.api.PSSDK
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.qmuiteam.qmui.widget.dialog.QMUIDialog


class HomeOneFragment : BaseFragment<FragmentHomeOneBinding, AddressViewModel>() {

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }

    override fun initView() {
        PSSDK.setContentLanguage("zh_hant")
        val layoutManager = GridLayoutManager(context, 2)
        binding.videoRecyclerView.layoutManager = layoutManager
        mViewModel.adapter = DramaListAdapter { index ->
            val shortPlay: ShortPlay? = mViewModel.videoResult.value?.get(index)
            context?.let { mViewModel.showDetailPage(it, shortPlay) }
        }
        binding.videoRecyclerView.adapter = mViewModel.adapter
    }
    override fun initData(savedInstanceState: Bundle?) {
        mViewModel.queryPopularVideo()
        mViewModel.videoResult.observe(requireActivity()) {
            mViewModel.adapter.addData(it)
        }
    }
    companion object {
        @JvmStatic
        fun newInstance() = HomeOneFragment()
    }

    private fun privacyCompliance() {
        if (SPUtils.getInstance().getBoolean(Constant.SP_GAO_DE_AGREEMENT)) {
            return
        }
        val spannable =
            SpannableStringBuilder("\"亲，感谢您对\"短视频\"一直以来的信任！我们依据最新的监管要求更新了\"短视频\" 《隐私权政策》，特向您说明如下\n1.为向您提供交易相关基本功能，我们会收集、使用必要的信息；\n2.基于您的明示授权，我们可能会获取您的位置（为您提供附近的商品、店铺及优惠资讯等）等信息，您有权拒绝或取消授权；\n3.我们会采取业界先进的安全措施保护您的信息安全；\n4.未经您同意，我们不会从第三方处获取、共享或向提供您的信息；\n")
        spannable.setSpan(
            ForegroundColorSpan(Color.BLUE),
            42,
            49,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        QMUIDialog(context).setTitle("温馨提示(隐私合规示例)")
        AlertDialog.Builder(context)
            .setTitle("温馨提示(隐私合规示例)")
            .setMessage(spannable)
            .setPositiveButton(
                "同意"
            ) { _, _ ->
                SPUtils.getInstance().put(Constant.SP_GAO_DE_AGREEMENT, true)
            }
            .setNegativeButton(
                "不同意"
            ) { _, _ ->
                SPUtils.getInstance().put(Constant.SP_GAO_DE_AGREEMENT, true)
            }
            .show()
    }

    override fun onVisibleExceptFirst() {
        super.onVisibleExceptFirst()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }
}